package com.why.controller;


import com.why.entity.Result;
import com.why.entity.Stu;
import com.why.service.IStuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hy-woo
 * @since 2022-05-30
 */
@RestController
@RequestMapping("/stu")
public class StuController {

    @Autowired
    IStuService stuService;

    @RequestMapping("/update")
    public Result update(@RequestBody Stu stu){
        boolean b = stuService.updateById(stu);
        if (b){
            return new Result<String>(200,"","更新成功");
        }else {
            return new Result<String>(400,"","更新失败");
        }
    }

    @RequestMapping("/getStu")
    public Result getStu(int id){
        System.err.println(id);
        Stu stu = stuService.getById(id);
        return new Result<Stu>(200,"",stu);
    }

    @RequestMapping("/del")
    public Result del(String ids){
        boolean b = false;
        String[] idss = ids.split(",");
        for (String s : idss) {
            b = stuService.removeById(s);
        }
        if (b){
            return new Result<String>(200,"","删除成功");
        }else {
            return new Result<String>(400,"","删除失败");
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Stu stu){
        boolean b = stuService.saveOrUpdate(stu);
        if (b){
            return new Result<String>(200,"","添加成功");
        }else {
            return new Result<String>(400,"","添加失败");
        }
    }

}
